package com.ddyx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ddyx.entity.GoodsCategory;
import com.ddyx.entity.GoodsTag;
import com.ddyx.mapper.GoodsCategoryMapper;
import com.ddyx.service.GoodsCategoryService;
import com.ddyx.util.ResponseForm;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
@Slf4j
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

	@Resource
	GoodsCategoryMapper gcMapper;

	@Override
	public ResponseForm getCategory(Integer categoryId) {
		ResponseForm result = new ResponseForm();
		try {
			Example example = new Example(GoodsCategory.class);
			example.setOrderByClause("category_sort DESC");
//			example.setOrderByClause("DESC");
			Criteria criteria = example.createCriteria();
			criteria.andEqualTo("categoryStatus", 0);
			// categoryId为空时,默认查询最高层级分类,pid为0
			if (categoryId == null) {
				// c.setCategoryPid(0);
				criteria.andEqualTo("categoryPid", 0);
				List<GoodsCategory> cList = gcMapper.selectByExample(example);
				result.setData(cList);
				return result;
			}
			
			// 如果categoryId不为空,查询pid为当前categoryId的子分类
			if(categoryId != null) {
				criteria.andEqualTo("categoryPid", categoryId);
				List<GoodsCategory> cList = gcMapper.selectByExample(example);
				result.setData(cList);
				if (cList.size()<1) {
					result.setMessage("当前分类无子分类");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Override
	public ResponseForm getCategoryById(Integer categoryId) {
		ResponseForm result = new ResponseForm();
		if(categoryId!=null) {
			GoodsCategory gc = new GoodsCategory();
			gc.setCategoryId(categoryId);
			try {
				gc = gcMapper.selectByPrimaryKey(gc);
				result.setData(gc);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		
		}
		return result;
	}

}
